package l1j.server.server;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.configure.Config;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;

public class AinAndFeatherController implements Runnable {
	private static Logger _log = Logger.getLogger(FishingTimeController.class.getName());
	private static AinAndFeatherController _instance;

	public static AinAndFeatherController getInstance() {
		if (_instance == null) {
			_instance = new AinAndFeatherController();
		}
		return _instance;
	}

	@Override
	public void run() {
		while (true) {
			try {
				for (L1PcInstance _pc : L1World.getInstance().getAllPlayers()) {
					if (_pc == null || _pc.getNetConnection() == null) {
						continue;
					} else {
						int timeCount = _pc.getTimeCount();
						int featherTime = (Config.FEATHER_TIME)-1;

						if (timeCount >= featherTime) {
							giveFeather(_pc);
						} else {
							_pc.setTimeCount(timeCount + 1);
						}
					}
					
					if(_pc.getLevel() >= 49){
						int sc = _pc.getSafeCount();
						if(_pc.getZoneType() == 1 && !_pc.isPrivateShop()) {
							if(sc >= 14){
								if(_pc.getAinHasad() <= 1999999)
									_pc.calAinHasad(10000 );
								_pc.setSafeCount(0);
							} else {
								_pc.setSafeCount(sc+1);
							}
						} else {
							if(sc > 0)
								_pc.setSafeCount(0);
						}
					}

				}
				Thread.sleep(60000);
			} catch (Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}

		}
	}

	private void giveFeather(L1PcInstance pc) {
		pc.setTimeCount(0);
		int count = Config.FEATHER_NUMBER;
		int featherCount = count;
		pc.getInventory().storeItem(41159, featherCount);
		pc.sendPackets(new S_SystemMessage("신비한 날개 깃털 ("+featherCount	+")개를 얻었습니다."));
	}

}
